﻿using ERP.ERPModels.Production.MasterData;
using ERP.ERPModels.WarehouseManagement;
using ERP.HumanResources.HumanResourceManagement.Dtos;
using ERP.MasterData.Dtos;
using ERP.WarehouseManagement.Dto;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace ERP.WarehouseManagement
{
    public class OutsourcingService:ApplicationService, IOutsourcingService
    {
        private readonly IRepository<Outsourcing> _outsourcingRepository;

        private readonly IRepository<Material, int> _materialRepository;

        public OutsourcingService(IRepository<Outsourcing> outsourcingRepository, IRepository<Material, int> materialRepository)
        {
            _outsourcingRepository = outsourcingRepository;
            _materialRepository = materialRepository;
        }

        /// <summary>
        /// 获取物料列表
        /// </summary>
        public async Task<List<Material>> GetListAsync()
        {
            var list = await _materialRepository.GetQueryableAsync();

            return list.ToList();
        }

        /// <summary>
        /// 创建外协入库
        /// </summary>
        /// <param name="input">创建参数</param>
        /// <returns></returns>1
        public async Task<int> AddOutsourcing(OutsourcingAddDTO param)
        {
            var result = ObjectMapper.Map<OutsourcingAddDTO, Outsourcing>(param);
            var query = await _outsourcingRepository.InsertAsync(result);
            return query == null ? 0 : 1;
        }

        /// <summary>
        /// 更新外协入库
        /// </summary>
        /// <param name="id">ID</param>
        /// <param name="input">更新参数</param>
        /// <returns></returns>
        public async Task<int> UpdateOutsourcing(OutsourcingAddDTO param)
        {
            param.DocumentStatus = 2;
            var result = ObjectMapper.Map<OutsourcingAddDTO, Outsourcing>(param);
            
            var query = await _outsourcingRepository.UpdateAsync(result);
            return query == null ? 0 : 1;
        }

        /// <summary>
        /// 删除外协入库
        /// </summary>
        /// <returns></returns>
        public async Task<int> DeleteOutsourcing(int Id)
        {
            try
            {
                var result = await _outsourcingRepository.FindAsync(m => m.Id == Id);
                if (result == null)
                {
                    return 0;
                }
                await _outsourcingRepository.DeleteAsync(result);
                return 1;
            }
            catch
            {
                return 0;
            }
        }
        /// <summary>
        /// 执行入库
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<int> UpdateIsLock(int Id)
        {
            var result = await _outsourcingRepository.FindAsync(m => m.Id == Id);
            if (result == null)
            {
                return 0;
            }
            result.DocumentStatus = 3;

            var query = await _outsourcingRepository.UpdateAsync(result);
            return query == null ? 0 : 1;
        }
    }
}
